package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.thrd.basicframework.http.OkHttpUtils;
import com.thrd.basicframework.http.callback.BitmapCallback;
import com.thrd.basicframework.utils.LogUtil;
import com.thrd.taiexhibition.bean.BeanCityIndustry;
import com.thrd.taiexhibition.bean.BeanCityIndustryCoordinate;

import okhttp3.Call;

/**
 * @author: ${唐杰}
 * @date: 2021/3/25
 */
public class TouchImageView extends ImageView {
    public TouchImageView(@NonNull Context context) {
        super(context);
    }

    public TouchImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private Handler mHandler=new Handler(Looper.getMainLooper());

    private BeanCityIndustry mBeanCityIndustry;
    public  void setTouchData(final BeanCityIndustry mBeanCityIndustry){
        this.mBeanCityIndustry=mBeanCityIndustry;
      //  setControllerListener(this, Constant.IMAGE+mBeanCityIndustry.getIndustryLocationImg(),getContext());
    }




    private int touchX;
    private int touchY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchX = (int) event.getX();
                touchY = (int) event.getY();
                //   Log.e(TAG,"touchX="+touchX);
                break;

            case MotionEvent.ACTION_UP:
                int x = (int) event.getX();
                int y = (int) event.getY();
                LogUtil.e("xyxy","x="+x+"y="+y);
//                if ((Math.abs(x - touchX) < 30) && (Math.abs(y - touchY) < 30)&&onTouchImageListener!=null) {
//                    for (BeanCityIndustryCoordinate mCoordinate :mBeanCityIndustry.getCityIndustryList()){
//                        if(isArea(x,y,mCoordinate)){
//                            onTouchImageListener.onTouchImageListener(mCoordinate);
//                            break;
//                        }
//                    }
//
//
//                }

                break;


            default:
                break;
        }

        return true;
    }

    private boolean isArea(float x,float y,BeanCityIndustryCoordinate mCoordinate){
        int width=mCoordinate.getWidth()/2;
        int hight=mCoordinate.getLength()/2;
        if(Math.abs(x- (mCoordinate.getxAxis()+width))<width&&Math.abs(y- (mCoordinate.getyAxis()+hight))<hight){
            return true;
        }
        return false;
    }



    /**通过imageWidth 的宽度，自动适应高度
     * @param simpleDraweeView view
     * @param imagePath  Uri
     //* @param imageWidth width
     * @param mContext  context
     */
    private   void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, Context mContext) {
        OkHttpUtils.get().url("").build().buildCall(new BitmapCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(Bitmap response, int id) {

            }

            @Override
            public Bitmap onJson(String json) throws Exception {
                return null;
            }
        });
        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                if (imageInfo == null) {
                    return;
                }
                int height = imageInfo.getHeight()*2;
                int width = imageInfo.getWidth()*2;
                ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
                layoutParams.width = width;//imageWidth;
                layoutParams.height = height;//(int) ((float) (imageWidth * height) / (float) width);
                simpleDraweeView.setLayoutParams(layoutParams);
            }

            @Override
            public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                Log.d("TAG", "Intermediate image received");
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                throwable.printStackTrace();
            }
        };

        Log.d("TAG", "iamgepath"+imagePath);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setControllerListener(controllerListener)
                .setUri(Uri.parse(imagePath))
                .build();
        simpleDraweeView.setController(controller);
    }

    private   void setControllerListener(final ImageView mImageView, String imagePath, Context mContext) {
        OkHttpUtils.get().url(imagePath).build().execute(new BitmapCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                LogUtil.e("Image","onError");
            }

            @Override
            public void onResponse(Bitmap response, int id) {
                LogUtil.e("Image","onError");
                mImageView.setImageBitmap(response);
            }

            @Override
            public Bitmap onJson(String json) throws Exception {
                return null;
            }


        });
    }


    private OnTouchImageListener onTouchImageListener;

    public OnTouchImageListener getOnTouchImageListener() {
        return onTouchImageListener;
    }

    public void setOnTouchImageListener(OnTouchImageListener onTouchImageListener) {
        this.onTouchImageListener = onTouchImageListener;
    }

    public interface OnTouchImageListener{

         void onTouchImageListener(BeanCityIndustryCoordinate mBeanCityIndustryCoordinate);
    }


}
